$(function () {
    $("#page").load(URI + "/rbac/html/page.html");
});

//查询所有数据
function findAll() {
    //trim()去掉两边的空格
    let data = {
        id: sessionStorage.getItem("userId"),
        nickName: $("#nickName").val(),
        phone: $("#phone").val(),
        degree: $("#degree").val(),
        sex: $("#sex").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    let result = myAjax("/api/dw/daily/getStudent", data, "post");
    // 总条数
    if (result) {
        $("#totalCount").html(result.data.count);
        setData(result.data.pageInfo.list);
    }else{
        alert(result.message)
    }
}

//遍历查询到的数据
function setData(data) {
    let html = "";
    for (let i = 0; i < data.length; i++) {
        html += " <tr >\n" +
            "            <td>" + data[i].id + "</td>\n" +
            "            <td title='" + data[i].nickName + "'>" + clearNull(data[i].nickName) + "</td>\n" +
            "            <td title='" + data[i].icon + "'><img src='" + clearNull(URI + IMG + data[i].icon) + "' style='width: 30px; height: 30px;' ></td>\n" +
            "            <td title='" + data[i].sex + "'>" + clearNull(data[i].sex) + "</td>\n" +
            "            <td title='" + data[i].age + "'>" + clearNull(data[i].age) + "</td>\n" +
            "            <td title='" + data[i].phone + "'>" + clearNull(data[i].phone) + "</td>\n" +
            "            <td title='" + data[i].email + "'>" + clearNull(data[i].email) + "</td>\n" +
            "            <td title='" + data[i].sno + "'>" + clearNull(data[i].sno) + "</td>\n" +
            "            <td title='" + data[i].className + "'>" + clearNull(data[i].className) + "</td>\n" +
            "            <td title='" + data[i].degree + "'>" + clearNull(data[i].degree) + "</td>\n" +
            "            <td title='" + data[i].dailyStatus + "'>" + clearNull(data[i].dailyStatus === '草稿' || data[i].dailyStatus === '未提交'? '<span style="color: red;">未提交</span>' : data[i].dailyStatus) + "</td>\n" +
            "          </tr>";
    }
    $("#tbody").html(html);
}


/***
 * 清除数据中的undefined和null将其置换为空，不显示
 */

function clearNull(value) {
    return value === "null" || value === undefined ? "" : value;
}


/***
 * 删除功能
 */
function del(id) {
    let result = myAjax("/api/dw/daily/del", {id: id}, "delete");
    if (result.code === 200) {
        //删除成功，就重新查询数据
        alert("删除成功(== ^^ ==)!");
        findAll();
    } else {
        alert("删除失败(╥╯^╰╥)！")
    }
}

/**
 * 编辑操作
 */
function toEditPage(id) {
    sessionStorage.setItem("dailyId", id);
    x_admin_show('编辑', URI + '/rbac/html/daily/mydaily-update.html', 800, 850);
}


/**
 * 提交日报
 */
function save(id) {
    let result = myAjax("/api/dw/daily/findById", {id: id}, "post");
    let status = result.data.status;
    if (status === "草稿") {//草稿状态为未提交状态
        status = "已提交";
        let data = {
            id: id,
            status: status
        };
        let info = myAjax("/api/dw/daily/update", data, "post");
        if (info.data === "isHave") {
            alert(info.message);
        }
        if (info.data === 1) {
            alert("提交成功！！！");
            x_admin_father_reload();
        }
        alert("提交失败！！！");
    }else{
        alert("不允许重复提交！！！");
    }
}

